package com.mb.app.appused.views;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MetroView extends ScrollView {

	private TableLayout tableView;
	private Context context;

	public int margin = 20;
	private ClickChild onClick;

	public List<View> views = new ArrayList<View>();

	public MetroView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MetroView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MetroView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		init(context);
	}

	@SuppressLint("NewApi")
	public MetroView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
		init(context);
	}

	/**
	 * 初始化
	 * 
	 * @param context
	 */
	@SuppressWarnings("unused")
	void init(Context context) {
		LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		tableView = new TableLayout(context);
		tableView.setLayoutParams(par);
		tableView.setShrinkAllColumns(true);
		this.addView(tableView);
		this.context = context;

		int width, height;
		DisplayMetrics dm = getResources().getDisplayMetrics();
		width = dm.widthPixels;
		height = dm.heightPixels;

	}

	public int getMargin() {
		return margin;
	}

	public void setMargin(int margin) {
		this.margin = margin;
	}

	public ClickChild getOnClick() {
		return onClick;
	}

	public void setOnClick(ClickChild onClick) {
		this.onClick = onClick;
	}

	/**
	 * 多表布局控制
	 * 
	 * @param row
	 * @return
	 */
	public TableLayout addTable(TableRow row) {
		TableLayout table = new TableLayout(context);
		table.setShrinkAllColumns(true);
		setSpan(row, 1);
		setSpan(table, 1);
		row.addView(table);
		return table;
	}

	/**
	 * 添加一行
	 * 
	 * @return
	 */
	public TableRow addRow() {
		TableRow row = new TableRow(context);
		tableView.addView(row);
		return row;
	}

	public TableRow addRow(TableLayout table) {
		TableRow row = new TableRow(context);
		setSpan(row, 1);
		table.addView(row);
		return row;
	}

	/**
	 * 添加一个控件
	 * 
	 * @param v
	 */
	public void addTableView(View view) {
		tableView.addView(view);
		setSpan(view, 1);
		views.add(view);
	}

	public void addTableView(TableLayout table, View view) {
		table.addView(view);
		setSpan(view, 1);
		views.add(view);
	}

	/**
	 * 根据ID
	 * 
	 * @param id
	 */
	public View addTableView(int id) {
		LayoutInflater inflater = LayoutInflater.from(this.context);
		View view = inflater.inflate(id, null);
		tableView.addView(view);
		setSpan(view, 1);
		views.add(view);
		return view;
	}

	public View addTableView(TableLayout table, int id) {
		LayoutInflater inflater = LayoutInflater.from(this.context);
		View view = inflater.inflate(id, null);
		table.addView(view);
		setSpan(view, 1);
		views.add(view);
		return view;
	}

	/**
	 * 添加一个控件到行内
	 * 
	 * @param row
	 * @param v
	 */
	public void addRowView(TableRow row, View view) {
		row.addView(view);
		setSpan(view, 1);
		views.add(view);
	}

	/**
	 * 添加一个控件到行内
	 * 
	 * @param row
	 * @param v
	 */
	public void addRowView(TableRow row, View view, int span) {
		row.addView(view);
		setSpan(view, span);
		views.add(view);
	}

	/**
	 * 添加控件
	 * 
	 * @param row
	 * @param id
	 * @return
	 */
	public View addRowView(TableRow row, int id) {
		LayoutInflater inflater = LayoutInflater.from(this.context);
		View view = inflater.inflate(id, null);
		row.addView(view);
		setSpan(view, 1);
		views.add(view);
		return view;
	}

	public View addRowView(TableRow row, int id, int span) {
		LayoutInflater inflater = LayoutInflater.from(this.context);
		View view = inflater.inflate(id, null);
		setSpan(view, span);
		row.addView(view);
		views.add(view);
		return view;
	}

	public void commit() {
		for (int i = 0; i < views.size(); i++) {
			final int sp = i;
			views.get(i).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (onClick != null) {
						onClick.onClick(sp);
					}
				}
			});
		}
	}

	void setSpan(View view, int span) {
		TableRow.LayoutParams tlp = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
				span);
		tlp.setMargins(margin, margin, margin, margin);
		view.setLayoutParams(tlp);
	}

	public interface ClickChild {
		public void onClick(int position);
	}

}
